<template>
  <div class="exam-detail">
    <div style="height: 40px">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/testQuestion' }"
          >测试题库</el-breadcrumb-item
        >
        <el-breadcrumb-item :to="{ path: '/exam' }"
          >考试记录</el-breadcrumb-item
        >
        <el-breadcrumb-item>考试详情</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div
      class="test-table"
      v-loading="tableLoading"
      element-loading-text="拼命加载中"
    >
      <!-- 表格 -->
      <el-table :data="examDetailList">
        <el-table-column
          type="index"
          width="50"
          :index="indexConfig"
          label="序号"
          align="center"
        ></el-table-column>
        <el-table-column
          label="考试题目"
          prop="examTopic"
          width="330"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          label="正确答案"
          prop="topicRightOption"
          width="330"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          label="用户答案"
          prop="topicUserOption"
          width="330"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          label="题目分值"
          prop="topicScore"
          width="120"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.status === 1 ? 'success' : 'danger'"
              disable-transitions
            >
              {{ scope.row.status === 1 ? "正确" : "错误" }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-show="examDetailList.length > 0"
        layout="prev, pager, next"
        :current-page="currentPage"
        @current-change="handleCurrentChange"
        :page-size="pageSize"
        :total="total"
        :page-count="7"
        style="text-align: right; margin-top: 20px"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getExamRecordDetail } from "@/api/client/examDetail";

export default {
  name: "ExamDetailView",
  props: ["id"],
  data() {
    return {
      page: 1,
      pageSize: 10,
      total: 0,
      currentPage: 1,
      examDetailList: [],
      tableLoading: false,
    };
  },
  created() {
    this.getExamDetail();
  },
  methods: {
    // 查询考试记录详情
    async getExamDetail() {
      try {
        this.tableLoading = true;
        const res = await getExamRecordDetail({
          page: this.page,
          pageSize: this.pageSize,
          examId: this.id,
        });
        this.examDetailList = res.data.records;
        this.total = res.data.total;
      } catch {
        this.$message.error("获取考试记录详情失败");
      } finally {
        this.tableLoading = false;
      }
    },
    // 配置表格索引
    indexConfig(index) {
      return (this.currentPage - 1) * this.pageSize + index + 1;
    },
    // 分页查询
    handleCurrentChange(page) {
      this.page = page;
      this.currentPage = page;
      this.getExamDetail();
    },
  },
};
</script>

<style lang="less" scoped>
.exam-detail {
  width: 100%;
  height: 100%;
  padding: 10px 60px;
  background-color: #fff;
  .test-table {
    width: 96%;
    height: 100%;
    margin: 0 auto;
    padding: 20px;
    box-shadow: 1px 1px 8px rgba(0, 0, 0, 0.15);
    transition: 0.2s;
    &:hover {
      box-shadow: 1px 1px 12px rgba(0, 0, 0, 0.2);
    }
  }
}
</style>